//
//  PhoneNumberListVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/30.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import LJContactManager

class PhoneNumberListVC: BaseViewController {
    
    var dataSetionModels : [LJSectionPerson]?
    
    var keys : [String]?
    
    var block : ((_ name : String,_ phoneNumber : String) -> ())?


    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "联系人列表"
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: .plain, target: self, action: #selector(dissMissYou))
        
        plainTableView.tableFooterView = UIView()
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MyContactsCell.self), bundle: nil), forCellReuseIdentifier: MyContactsCell.identifer())
        
          weak var weakSelf = self
         LJContactManager.sharedInstance().accessSectionContactsComplection { (succeed, persons, keys) in
            
            
            if persons != nil{
            
             weakSelf?.dataSetionModels = persons
                
             weakSelf?.keys = keys

                
             weakSelf?.plainTableView.reloadData()
                
            
            }
            

            
            
        }

    }
    
    
    
    
    func dissMissYou() {
       
        dismiss(animated: true) { 
            
            
        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

 
}






// MARK:- tableview代理方法
extension PhoneNumberListVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataSetionModels == nil {
            return 0
        }
        
      
        return dataSetionModels!.count
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataSetionModels == nil {
            return 0
        }

        return dataSetionModels![section].persons.count
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let tableCell : MyContactsCell =  tableView.dequeueReusableCell(withIdentifier: MyContactsCell.identifer()) as! MyContactsCell
        
        tableCell.model = dataSetionModels![indexPath.section].persons[indexPath.row]
        
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 72
    }
    
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        if dataSetionModels == nil {
            return ""
        }

        
        return dataSetionModels![section].key
    }
    
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        
            if keys != nil {
                    return keys
            }else {
                    return nil
                }
        
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 36
    }
    

    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        if  block == nil {
            dismiss(animated: true, completion: nil)
            
            return
        }
        
        let model = dataSetionModels![indexPath.section].persons[indexPath.row]

        if let temp  = model.phones.first?.phone {
            
            block!(model.fullName,temp)

        }
        
        dismiss(animated: true, completion: nil)

        
        
        
    }
    
    
    
}

